<?php

class ContactLogic {

  function save(&$contact)
  {
  	$contactDao = new ContactDAO();

  	return $contactDao->save($contact) == 1;
  }

  function load($id)
  {
  	$contactDAO = new ContactDAO();
  	return $contactDAO->get($id);
  }

  function tagAsType ($contact_id, $type_id)
  {
  	 $contactTypeMapDao = new ContactTypeMapDAO();

  	 $results = $contactTypeMapDao->findWhere("contact_id = $contact_id and contact_type = $type_id");
  	 if (count($results) == 0)
  	 {
  	 	$contactTypeMap = new ContactTypeMap(0, $contact_id, $type_id);
  	 	$contactTypeMapDao->save($contactTypeMap);
  	 }
  }

  function untagAsType ($contact_id, $type_id)
  {
  	 $contactTypeMapDao = new ContactTypeMapDAO();

  	 $results = $contactTypeMapDao->findWhere("contact_id = $contact_id and contact_type = $type_id");
  	 if (count($results) >0)
  	 {
  	 	$contactTypeMap = new ContactTypeMap(0, $contact_id, $type_id);
  	 	foreach ($results as $contactTypeMap)
  	 	{
  	 		$contactTypeMapDao->delete($contactTypeMap->id);
  	 	}
  	 }
  }

  function getTagsForContact ($contact_id) {
  	 $contactTypeMapDao = new ContactTypeMapDAO();

  	 $results = $contactTypeMapDao->findWhere("contact_id = $contact_id");

  	 return $results;
  }

  function delete($id)
  {
  	 $contactTypeMapDao = new ContactTypeMapDAO();
  	 $tags = $contactTypeMapDao->findWhere("contact_id = $id");
  	 print_r($tags);
  	 foreach ($tags as $tag) {
  	 	$contactTypeMapDao->delete($tag);
  	 }
  	 $contactDAO = new ContactDAO();
  	 $contact = new Contact($id);
  	 $contactDAO->delete($contact);
  }

  function findAll()
  {
    $contactDao = new ContactDAO();
    return $contactDao->findWhere("", "lastname, firstname");
  }

  function findByName ($name)
  {
  	$contactDao = new ContactDAO();
  	return $contactDao->findWhere("firstname like '%$name%' or lastname like '%$name%' or concat(firstname, ' ', lastname) like '%$name%'");
  }

  function findByCity ($city)
  {
  	 $contactDao = new ContactDAO();
  	 return $contactDao->findBySQL("select c.* from contact c, address a where c.address_id = a.id and a.city like '$city%'");
  }

  function findByZip ($zip, $miles=0)
  {
  	 // TODO right now we don't have any use of the "$miles" parameter because we need to look up all of the
  	 //     zip codes within X miles  -- Uze Zipdy and http://panda.unm.edu/ahedges/zip_lookup/
  	 //     How time-expensive is this?

  	 return array();
  }

  function findByExample ($contact, $address = null)
  {
  	 $contactDao = new ContactDAO();
  	 if ($address != null)
  	 {
  	 	$addressDao = new AddressDAO();
  	 	$addresses = $addressDao->findExample($address);

  	 	$all = array();

  	 	foreach ($addresses as $addresult)
  	 	{
  	 		$contact->address_id = $addresult->id;
  	 		foreach ($contactDao->findExample($contact) as $result)
  	 		{
  	 			array_push($all, $result);
  	 		}
  	 	}

  	 	return $all;
  	 }
  	 else
  	 {
  	 	return $contactDao->findExample($contact);
  	 }
  }

  function emailContact ($contact, $email)
  {
  	 // TODO figure out how to e-mail someone.
  }
}

?>
